package main

import (
	"chatroom/common/message"
	"chatroom/server/process"
	"chatroom/server/utils"
	"fmt"
	"io"
	"net"
)

type Processor struct {
	Conn net.Conn
}

func (this *Processor) serviceProcessMes(mes *message.Message) (err error) {
	switch mes.MsgType {
	case message.RegisterReqType:
		// 处理注册
	case message.LoginReqType:
		// 处理登录
		up := process.UserProcess{
			Conn: this.Conn,
		}
		err = up.ServerProcessLogin(mes)
	default:
		fmt.Println("消息类型不存在，无法处理...")
	}

	return
}

func (this *Processor) processor() (err error) {
	// 循环读取客户端发送的消息
	for {
		tf := utils.Transfer{
			Conn: this.Conn,
		}

		mes, err := tf.ReadPkg()

		if err != nil {
			if err == io.EOF {
				fmt.Println("客户端退出，服务端停止该conn处理...")
				return err
			} else {
				fmt.Println("readPkg err=", err)
				return err
			}
		}

		err = this.serviceProcessMes(&mes)
		if err != nil {
			// 客户端退出了，服务器停止处理conn的消息
			return err
		}
	}
}
